package com.yunji.base.business;

import com.google.common.collect.Lists;
import com.yunji.base.consumer.BaseDataConsumer;
import com.yunji.base.domain.BelongDO;
import com.yunji.base.domain.ManagerDO;
import com.yunji.base.dto.AgentDTO;
import com.yunji.base.dto.AgentFieldDTO;
import com.yunji.base.enums.AgentMode;
import com.yunji.base.enums.ScaleTypes;
import com.yunji.base.service.*;
import com.yunji.cabinet.bo.CabinetBO;
import com.yunji.cabinet.bo.CabinetCountBO;
import com.yunji.cabinet.bo.CabinetListQueryBO;
import com.yunji.cabinet.enums.UseStatus;
import com.yunji.cabinet.service.ICabinetService;
import com.yunji.common.base.BaseBusiness;
import com.yunji.common.constant.LogicCode;
import com.yunji.common.enums.common.ErrorEnum;
import com.yunji.common.enums.common.UserType;
import com.yunji.common.model.domain.AgentDO;
import com.yunji.common.model.domain.StaffDO;
import com.yunji.common.model.domain.SysUserDO;
import com.yunji.common.model.dto.LoginInfo;
import com.yunji.common.utils.DecimalUtils;
import com.yunji.common.utils.SecurityUtils;
import com.yunji.common.utils.bean.BeanUtils;
import com.yunji.order.bo.ExistsOrderBO;
import com.yunji.order.emuns.OrderStatusEnum;
import com.yunji.product.bo.ProductAgentUpdateBO;
import com.yunji.product.bo.ProductListQueryBO;
import com.yunji.product.domain.ProductDO;
import com.yunji.product.domain.ProductAgentDO;
import com.yunji.product.enums.GrantTypes;
import com.yunji.product.service.IProductAgentService;
import com.yunji.product.service.IProductService;
import com.yunji.system.service.ISysConfigService;
import com.yunji.system.service.ISysRoleService;
import com.yunji.system.service.TokenService;
import com.yunji.system.service.impl.SysUserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Project: yunji-coffee
 * @Package com.yunji.user.business
 * @FileName AgentBusiness.java
 * @Copyright Copyright © 2024 HuaAo0803@com. All Rights Reserved
 * @Company com
 * @Version 1.0
 * @Since JDK1.8.0_201
 * @CreateTime 2024-06-12 23:41:01
 * @Description 代理业务处理
 * @Modification History
 * Author					History Time			Version			Description
 * -------------------------------------------------------------------------------
 * HuaAo0803@163.com	2024-06-12 23:41:01		1.0				1.0
 */
@Component
public class AgentBusiness extends BaseBusiness {
    @Resource
    SiteService siteService;

    @Resource
    StaffService staffService;

    @Resource
    AgentService agentService;

    @Resource
    ISysRoleService roleService;

    @Resource
    SysUserServiceImpl userService;

    @Resource
    ManagerService managerService;

    @Resource
    IProductService productService;

    @Resource
    ManagerSiteService managerSiteService;

    @Resource
    AgentSiteService agentSiteService;

    @Resource
    IProductAgentService productAgentService;

    @Resource
    ISysConfigService configService;

    @Resource
    TokenService tokenService;

    @Resource
    ICabinetService cabinetService;

    @Resource
    BelongService belongService;

    private static final String MIN_AGENT_SCALE = "sys.agent.minScale";

    private static final String MAX_AGENT_SCALE = "sys.agent.maxScale";

    private static final String MAX_CABINET_LIMIT = "sys.cabinet.maxLimit";

    private static final String ERROR_CREATE = "新增代理失败";
    private static final String ERROR_UPDATE = "修改代理失败";
    @Autowired
    private BaseDataConsumer baseDataConsumer;

//    public List<AgentDTO> search(AgentParam param) {
//        return agentService.search(param);
//    }

    public AgentDTO select(Long id) {
        return agentService.select(id);
    }

    /**
     * @param dto 代理商信息
     * @Title checkAgentInfo
     * @Desc 校验代理信息
     * @Date 2024-08-16 13:53:54.194
     */
    private void checkAgentInfo(AgentDTO dto) {
        String agentName = dto.getAgentName();
        logs.info("【代理信息】代理商名称信息:{}", agentName);
        Assert.isTrue(BeanUtils.isNotNull(agentName), ErrorEnum.CHECKED_311001.getText());
        Assert.isTrue((agentName.length() <= 15), ErrorEnum.CHECKED_311002.getText());

        String phone = dto.getAgentTel();
        logs.info("【代理信息】代理商手机号码:{}", phone);
        Assert.isTrue(BeanUtils.isNotNull(phone), ErrorEnum.CHECKED_111003.getText());
        Assert.isTrue(phone.matches("1\\d{10}"), ErrorEnum.CHECKED_111004.getText());

        String region = dto.getRegion();
        logs.info("【代理信息】代理商区域地址:{}", region);
        Assert.isTrue(BeanUtils.isNotNull(region), ErrorEnum.CHECKED_111005.getText());
        Assert.isTrue((region.length() <= 25), ErrorEnum.CHECKED_111006.getText());

        String password = dto.getPassword();
        logs.info("【代理信息】代理商密码:{}", password);
        if (BeanUtils.isNotNull(password)) {
            Assert.isTrue(password.matches("^[A-Za-z0-9]{6,12}$"), ErrorEnum.CHECKED_111008.getText());
        }

        Integer cabinetLimit = dto.getContainerLimit();
        logs.info("【代理信息】代理商货柜限制:{}", cabinetLimit);
        Assert.isTrue(BeanUtils.isNotNull(cabinetLimit), ErrorEnum.CHECKED_311003.getText());
        String maxCabinetLimit = configService.selectConfigByKey(MAX_CABINET_LIMIT);
        logs.info("【代理信息】系统货柜最大限制:{}", cabinetLimit);
        Integer maxAgentLimit = BeanUtils.getInteger(maxCabinetLimit, 9999);
        logs.info("【代理信息】实际货柜最大限制:{}", cabinetLimit);
        Assert.isTrue((cabinetLimit <= maxAgentLimit), ErrorEnum.CHECKED_311006.getText());

        final BigDecimal fullRatio = new BigDecimal(100);
        BigDecimal platformScale = dto.getPlatformScale();
        logs.info("【代理信息】平台分润比例:{}", platformScale);
        Assert.isTrue(BeanUtils.isNotNull(platformScale), ErrorEnum.CHECKED_311004.getText());
        Assert.isTrue(DecimalUtils.less(platformScale, fullRatio), ErrorEnum.CHECKED_311007.getText());

        BigDecimal agentScale = dto.getAgentScale();
        logs.info("【代理信息】代理商分润比例:{}", agentScale);
        Assert.isTrue(BeanUtils.isNotNull(agentScale), ErrorEnum.CHECKED_311005.getText());
        Assert.isTrue(!DecimalUtils.more(agentScale, fullRatio), ErrorEnum.CHECKED_311008.getText());
        Assert.isTrue(DecimalUtils.compare(agentScale.add(platformScale), fullRatio), ErrorEnum.CHECKED_311009.getText());

        Integer cooperateWay = dto.getCooperateWay();
        String cooperateText = AgentMode.get(cooperateWay);
        logs.info("【代理信息】合作方式{}:{}", cooperateWay, cooperateText);
        if (!BeanUtils.equals(cooperateWay, AgentMode.SELF_SUPPORT.getCode())) {
            return;
        }

        String minAgentScale = configService.selectConfigByKey(MIN_AGENT_SCALE);
        String maxAgentScale = configService.selectConfigByKey(MAX_AGENT_SCALE);
        logs.info("【代理信息】系统配置最小比例:{},最大比例:{}", minAgentScale, maxAgentScale);

        Assert.isTrue(BeanUtils.isNotNull(minAgentScale), "未找到系统配置最小比例");
        Assert.isTrue(BeanUtils.isNotNull(maxAgentScale), "未找到系统配置最高比例");

        BigDecimal minScale = new BigDecimal(minAgentScale);
        BigDecimal maxScale = new BigDecimal(maxAgentScale);
        logs.info("【代理信息】转换数据最小比例:{},最大比例:{}", maxScale, maxScale);

        Assert.isTrue(!DecimalUtils.more(agentScale, maxScale), "代理分润超出可配置最高值");
        Assert.isTrue(!DecimalUtils.less(agentScale, minScale), "代理分润低于可配置最小值");
    }

    /**
     * @param dto 代理信息
     * @Title handBelong
     * @Desc 处理所属信息
     * @Date 2024-08-19 18:04:16.196
     * @Return Belong 代理所属信息
     */
    private BelongDO handBelong(AgentDTO dto) {
        BelongDO belong = new BelongDO();
        belong.setBelongName(dto.getAgentName());
        belong.setBelongType(UserType.AGENT.getCode());
        belong.setScaleType(ScaleTypes.RATIO.getCode());

        Integer agentMode = dto.getCooperateWay();
        String agentText = AgentMode.get(agentMode);
        BigDecimal scaleRatio = BeanUtils.getDecimal(dto.getAgentScale());
        logs.info("【新增代理】代理模式={}:{},代理分润比例:{}", agentMode, agentText, scaleRatio);
        belong.setAgentMode(agentMode);
        belong.setScaleRatio(scaleRatio);

        BigDecimal fullRatio = new BigDecimal(100);
        BigDecimal deductRatio = fullRatio.subtract(scaleRatio);
        logs.info("【新增代理】扣减分润比例:{}", deductRatio);
        belong.setDeductRatio(deductRatio);
        Assert.isTrue(belongService.create(belong), ERROR_CREATE);
        return belong;
    }

    /**
     * @param dto 代理商信息
     * @Title createAccount
     * @Desc 创建代理用户信息
     * @Date 2024-08-16 11:52:55.848
     * @Return SysUser 用户信息
     */
    private SysUserDO createAccount(AgentDTO dto) {
        SysUserDO user = new SysUserDO();
        String phone = dto.getAgentTel();
        logs.info("【新增代理】添加登录账号:{}", phone);
        user.setUserName(phone);
        user.setPhonenumber(phone);
        user.setNickName(dto.getAgentName());

        user.setMainAccount(LogicCode.YES);
        Long belongId = dto.getBelongId();
        logs.info("【新增代理】账号归属ID:{}", belongId);
        user.setBelongId(belongId);

        String rawPassword = dto.getPassword();
        logs.info("【新增代理】账号密码:{}", rawPassword);
        String password = BeanUtils.isNotNull(rawPassword) ? rawPassword : phone.substring(phone.length() - 6);
        user.setPassword(SecurityUtils.encryptPassword(password));

        String roleType = UserType.AGENT.getCode();
        String roleText = UserType.getText(roleType);
        logs.info("【新增代理】角色类型={}:{}", roleType, roleText);

        // 根据权限标识符找到代理角色ID
        Long roleId = roleService.selectRoleByRoleCode(roleType);
        logs.info("【新增代理】代理角色ID:{}", roleId);
        Assert.isTrue(BeanUtils.isNotNull(roleId), "没找到代理角色ID");

        Long[] roles = {roleId};
        user.setRoleIds(roles);
        user.setUserType(roleType);
        return user;
    }

    /**
     * @param product 商品信息
     * @Title handlePriceLimit
     * @Desc 处理商品价格限制
     * @Date 2024-08-16 14:17:30.396
     * @Return BigDecimal 最终销售价格
     */
    private BigDecimal handlePriceLimit(ProductDO product) {
        BigDecimal discountPrice = product.getDiscountPrice();
        BigDecimal minDiscountPrice = product.getMinDiscountPrice();
        logs.info("【商品价格】商品价格：{}, 最小价格限制:{}", discountPrice, minDiscountPrice);
        discountPrice = DecimalUtils.less(discountPrice, minDiscountPrice) ? minDiscountPrice : discountPrice;

        BigDecimal maxDiscountPrice = product.getMaxDiscountPrice();
        logs.info("【商品价格】商品价格：{}, 最大价格限制:{}", discountPrice, maxDiscountPrice);
        discountPrice = DecimalUtils.more(discountPrice, maxDiscountPrice) ? maxDiscountPrice : discountPrice;

        logs.info("【商品价格】实际商品价格:{}", discountPrice);
        return discountPrice;
    }

    /**
     * @param dto 代理商信息
     * @Title create
     * @Desc 创建代理信息
     * @Date 2024-08-22 15:47:47.307
     */
    @Transactional
    public Long create(AgentDTO dto) {
        // 校验信息是否合法
        checkAgentInfo(dto);

        String phone = dto.getAgentTel();
        logs.info("【新增代理】代理手机号:{}", phone);
        SysUserDO check = new SysUserDO();
        check.setUserName(phone);
        Assert.isTrue(userService.checkUserNameUnique(check), ErrorEnum.BUSINESS_121001.getText());

        BelongDO belong = handBelong(dto);
        Long belongId = belong.getBelongId();
        logs.info("【新增代理】代理所属ID:{}", belongId);
        dto.setBelongId(belongId);

        SysUserDO user = createAccount(dto);

        AgentDO agent = new AgentDO();
        BeanUtils.clone(dto, agent);

        agent.setBelongId(belongId);
        Assert.isTrue(agentService.create(agent), ERROR_CREATE);

        Long agentId = agent.getId();
        logs.info("【新增代理】代理ID:{}", agentId);

        LoginInfo login = tokenService.getUser();
        Long currentBelongId = login.getBelongId();

        // 授权商品
        ProductListQueryBO param = new ProductListQueryBO();
        param.setGrantType(GrantTypes.ALL_GRANT.getCode());
        param.setBelongId(currentBelongId);
        List<ProductDO> list = productService.searchProductPrice(param);

        ArrayList<ProductAgentDO> createList = new ArrayList<>();
        for (ProductDO product : list) {
            ProductAgentDO productAgent = new ProductAgentDO();

            // 代理信息
            productAgent.setAgentId(agentId);
            productAgent.setUserType(UserType.AGENT.getCode());

            productAgent.setBelongId(belongId);
            // 产品信息
            Long productId = product.getId();
            productAgent.setProductId(productId);
            BigDecimal discountPrice = handlePriceLimit(product);
            logs.info("【新增代理】商品ID:{}, 折扣价格:{}", productId, discountPrice);

            BigDecimal salePrice = product.getPrice();
            BigDecimal costPrice = product.getCostPrice();
            logs.info("【新增代理】销售价格:{},成本价格:{}", salePrice, costPrice);

            productAgent.setPrice(salePrice);
            productAgent.setDiscountPrice(discountPrice);
            productAgent.setCostPrice(costPrice);

            productAgent.setAgentName(agent.getAgentName());
            productAgent.setProductName(product.getProductName());

            // 代理价格
            productAgent.setAgentPrice(discountPrice);

            // 初始状态
            productAgent.setStatus(UseStatus.ENABLE.getCode());
            productAgent.setDelFlag(UseStatus.ENABLE.getCode());

            createList.add(productAgent);
        }

        Assert.isTrue((userService.insertUser(user) > 0), ERROR_CREATE);
        Long userId = user.getUserId();
        logs.info("【新增代理】代理用户ID:{}", userId);

        createList.forEach(c -> productAgentService.create(c));

        BelongDO updateBelong = new BelongDO();
        updateBelong.setBelongId(belongId);
        // 数据对齐
        updateBelong.setParentId(belongId);
        updateBelong.setAgentId(agentId);
        updateBelong.setUserId(userId);
        Assert.isTrue(belongService.update(updateBelong), ERROR_CREATE);

        AgentDO updateAgent = new AgentDO();
        updateAgent.setId(agentId);
        updateAgent.setAgentId(userId);
        Assert.isTrue(agentService.update(updateAgent), ERROR_CREATE);

        return agentId;
    }

    /**
     * @param dto 代理
     * @Title handleManager
     * @Desc 修改网点分润
     * @Date 2024-08-17 17:13:30.571
     */
    public void handleManager(AgentDTO dto) {
        Integer cooperateWay = dto.getCooperateWay();
        String context = AgentMode.get(cooperateWay);
        logs.info("【代理网点】代理合作模式:{}={} ", cooperateWay, context);
        if (!Objects.equals(AgentMode.AGENT_SUPPORT.getCode(), cooperateWay)) {
            return;
        }

        Long parentId = dto.getBelongId();
        logs.info("【代理网点】代理商所属ID:{}", parentId);
        List<ManagerDO> managerList = managerService.searchAgent(parentId);
        if (BeanUtils.isNull(managerList)) {
            return;
        }

        BigDecimal agentScale = BeanUtils.getDecimal(dto.getAgentScale());
        for (ManagerDO manager : managerList) {
            BelongDO belong = new BelongDO();
            BigDecimal managerScale = BeanUtils.getDecimal(manager.getScale());
            logs.info("【代理网点】代理分成比例：{}, 网点分成比例:{}", agentScale, managerScale);
            if (!DecimalUtils.more(managerScale, agentScale)) {
                continue;
            }

            Long belongId = manager.getBelongId();
            BigDecimal deductRatio = agentScale.subtract(managerScale);
            logs.info("【代理网点】网点所属ID:{}, 扣除分成比例:{}", belongId, deductRatio);

            belong.setBelongId(belongId);
            belong.setScaleRatio(managerScale);
            belongService.update(belong);

            manager.setScale(agentScale);
            managerService.update(manager);
        }
    }

    /**
     * @param dto 代理商信息
     * @Title update
     * @Desc 更新代理信息
     * @Date 2024-08-23 14:10:58.573
     */
    @Transactional
    public void update(AgentDTO dto) {
        Long id = dto.getId();
        logs.info("【编辑代理】代理ID:{}", id);
        Assert.isTrue(BeanUtils.isNotNull(id), "未找到代理信息");

        AgentDTO origin = agentService.select(id);
        logs.info("【编辑代理】原始信息:{}", origin);
        Assert.isTrue(BeanUtils.isNotNull(origin), "未找到代理信息");

        String phone = dto.getAgentTel();
        logs.info("【编辑代理】手机号码:{}", phone);

        checkAgentInfo(dto);

        // 多个主账号无效
        Long agentId = dto.getAgentId();

        SysUserDO checkUser = new SysUserDO();
        checkUser.setUserId(agentId);
        checkUser.setUserName(phone);
        Assert.isTrue(userService.checkUserNameUnique(checkUser), ErrorEnum.BUSINESS_121001.getText());

        // 合作代理将 网点分润随代理波动
        Integer agentMode = dto.getCooperateWay();
        String agentText = AgentMode.get(agentMode);
        logs.info("【编辑代理】代理合作模式={}:{}", agentMode, agentText);

        // 校验当前货柜是否达到代理商上限
        int count = cabinetService.countCabinet(CabinetCountBO.builder().agentId(agentId).build());
        Integer limit = dto.getContainerLimit();
        logs.info("【编辑代理】货柜数量:{}, 货柜限制:{}", count, limit);
        Assert.isTrue((count <= limit), ErrorEnum.BUSINESS_312001.getText());

        // 找到该代理商修改信息
        SysUserDO user = userService.selectUserById(agentId);
        Assert.isTrue(BeanUtils.isNotNull(user), "未找到代理账号");
        user.setUserName(phone);
        user.setNickName(dto.getAgentName());

        Integer status = dto.getStatus();
        String password = dto.getPassword();
        logs.info("【编辑代理】用户状态:{}, 用户密码:{}", status, password);
        if (status != null) {
            user.setStatus(String.valueOf(status));
        }
        if (password != null) {
            user.setPassword(SecurityUtils.encryptPassword(password));
        }

        handleManager(dto);

        Long belongId = user.getBelongId();
        BigDecimal scaleRatio = BeanUtils.getDecimal(dto.getAgentScale());
        logs.info("【编辑代理】所属ID:{},代理分润比例:{}", belongId, scaleRatio);
        BelongDO belong = new BelongDO();
        belong.setBelongId(belongId);
        belong.setBelongName(dto.getAgentName());
        belong.setScaleRatio(scaleRatio);
        belong.setAgentMode(agentMode);

        BigDecimal fullRatio = new BigDecimal(100);
        BigDecimal deductRatio = fullRatio.subtract(scaleRatio);
        logs.info("【编辑代理】扣减分润比例:{}", deductRatio);
        belong.setDeductRatio(deductRatio);

        AgentDO agent = new AgentDO();
        BeanUtils.clone(dto, agent);

        Assert.isTrue(belongService.update(belong), ERROR_UPDATE);
        Assert.isTrue((userService.updateAgentUser(user) > 0), ERROR_UPDATE);
        Assert.isTrue(agentService.update(agent), ERROR_UPDATE);
    }

    /**
     * @param agentId 代理商ID
     * @Title handleSite
     * @Desc 处理网点信息
     * @Date 2024-09-19 16:06:34.520
     * @Return List<Long> 用户ID集合
     */
    private List<Long> handleSite(Long agentId) {
        logs.info("【删除代理商】代理ID:{}", agentId);
        List<BelongDO> list = belongService.listByAgentId(agentId);
        if (BeanUtils.isNull(list)) {
            return Collections.emptyList();
        }

        List<Long> userIds = list.stream().map(BelongDO::getUserId).collect(Collectors.toList());
        List<Long> siteIds = list.stream().map(BelongDO::getSiteId).filter(BeanUtils::isNotNull).collect(Collectors.toList());
        for (Long siteId : siteIds) {
            Assert.isTrue(siteService.delete(siteId), "删除失败");
            Assert.isTrue(agentSiteService.delete(siteId), "删除失败");
            Assert.isTrue(managerSiteService.deleteBySiteId(siteId), "删除失败");
        }

        managerService.deleteByAgentId(agentId);
        //FIXME 删除代理审核 siteAuditService.deleteByAgentId(agentId);
        return userIds;
    }

    /**
     * @param id 代理商ID
     * @Title delete
     * @Desc 删除代理信息
     * @Date 2024-08-23 14:12:01.084
     */
    @Transactional
    public void delete(Long id) {
        logs.info("【删除订单】代理ID:{}", id);
        AgentDTO agent = agentService.select(id);
        Assert.isTrue(BeanUtils.isNotNull(agent), "未找到代理商信息");

        Long belongId = agent.getBelongId();
        logs.info("【删除代理商】代理商ID:{}", belongId);

        // 查看当前代理关联的网点
        List<CabinetBO> cabinets = cabinetService.searchList(CabinetListQueryBO.builder().agentId(id).build());
        Assert.isTrue(BeanUtils.isNull(cabinets), "当前代理商下有货柜");

        // 查看当前代理关联的员工
        List<StaffDO> staffList = staffService.listByAgentId(id);
        Assert.isTrue(BeanUtils.isNull(staffList), "当前代理商下有员工");

        //查看和当前代理有关的交易成功订单数据
        boolean rs = baseDataConsumer.existsOrder(ExistsOrderBO.builder()
                .statusList(Lists.newArrayList(OrderStatusEnum.PAY_SUCCESS.getCode())).agentId(id).build());
        Assert.isTrue(!rs, "有订单数据不能删除");

        List<Long> userIds = handleSite(id);
        for (Long userId : userIds) {
            userService.deleteUserById(userId);
        }
        Assert.isTrue(agentService.delete(id), "删除失败");

        productAgentService.updateByBO(ProductAgentUpdateBO.builder().agentId(id).status(LogicCode.YES).build());

        Assert.isTrue(belongService.deleteById(belongId), "删除失败");
    }

    /**
     * @Title getAgentInfo
     * @Desc 获取代理信息
     * @Date 2024-08-23 10:34:33.823
     * @Return AgentDTO 代理商信息
     */
    public AgentDTO getAgentInfo() {
        LoginInfo user = tokenService.getUser();
        Assert.isTrue(BeanUtils.isNotNull(user), "请登录系统后访问");
        Long belongId = user.getBelongId();
        logs.info("【代理商信息】所属ID:{}", belongId);
        AgentDTO dto = agentService.selectBelong(belongId);
        Assert.isTrue(BeanUtils.isNotNull(dto), "未找到代理商信息");
        return dto;
    }

    /**
     * @Title getAgentScaleRatio
     * @Desc 获取当前代理分润比例
     * @Date 2024-08-23 17:36:05.170
     * @Return BigDecimal 代理分润比例
     */
    public BigDecimal getAgentScaleRatio() {
        LoginInfo user = tokenService.getUser();
        Long belongId = user.getBelongId();
        logs.info("【商品信息】当前用户所属ID:{}", belongId);

        AgentDTO agentDTO = agentService.selectBelong(belongId);
        if (agentDTO == null) {
            return null;
        }

        BigDecimal platformScale = agentDTO.getPlatformScale();
        return (new BigDecimal(100).subtract(platformScale));
    }

    /**------------------------------------------搜索栏------------------------------------------*/

    /**
     * @param name 名称信息
     * @Title searchName
     * @Desc 搜索匹配名称信息
     * @Date 2024-08-23 14:45:47.399
     * @Return List<String> 匹配名称集合
     */
    public List<String> searchName(String name) {
        Assert.isTrue((name.length() <= 15), "输入用户昵称不合规");
        AgentFieldDTO param = new AgentFieldDTO();
        param.setAgentName(name);
        param.setSearchFiled("agent_name");
        List<String> list = agentService.searchTableFiled(param);
        return BeanUtils.isNotNull(list) ? list : new ArrayList<>();
    }

    /**
     * @param phone 手机号码
     * @Title searchPhone
     * @Desc 搜索匹配手机号信息
     * @Date 2024-08-23 14:44:45.652
     * @Return List<String> 匹配手机号集合
     */
    public List<String> searchPhone(String phone) {
        Assert.isTrue(phone.matches("^\\d{0,11}$"), "输入手机号不合规");
        AgentFieldDTO param = new AgentFieldDTO();
        param.setAgentTel(phone);
        param.setSearchFiled("agent_tel");
        List<String> list = agentService.searchTableFiled(param);
        return BeanUtils.isNotNull(list) ? list : new ArrayList<>();
    }
}